// subpackagesA/pages/equipmentLedger/equipmentLedger.ts
import * as natApi from '../../../api/Ledger'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    option1: [
      { text: '供热公司', value: 0 },
    ],
    option2: [
      { text: '全部站点', value: 0 },
    ],
    option3: [
      { text: '类型', value: 0 },
      { text: '机组', value: 2 },
      { text: '换热站', value: 1 },
    ],
    value1: 0,
    value2: 0,
    value3: 1,
    list: [],
    isFresh: false as boolean,
    comID: 0,
    equipID:0,
    type: 1,
    page: 1,
    limit: 10,
    search: '',
    loading: false,      // 加载状态，防止重复请求
    hasMore: true        // 是否有更多数据
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad() {
    natApi.getcomany().then((res: any) => {
      let arr = this.data.option1
      res.data.map((item: any) => {
        arr.push({
          text: item.companyName,
          value: item.id
        })
      })
      this.setData({
        option1: arr
      })

    })
  },
  onClickLeft() {
    wx.navigateBack({
      delta: 1,
    });
  },
  onAdd() {
    wx.navigateTo({
      url: '/subpackagesA/pages/addLedger/addLedger'
    })
  },
  onComany(e: any) {
    this.setData({
      comID: e.detail.value
    })

  },
  onAction(e:any){
    this.setData({
      equipID: e.detail.value
    })
    this.onSearch();
  },
  onType(e: any) {
    const comID = this.data.comID;
    this.setData({
      type: e.detail.value,
      value2:0
    })
    let option2 = [
      { text: '全部站点', value: 0 },
    ]
    natApi.getHrz(comID, e.detail.value).then((res: any) => {
      console.log(res)
      if(this.data.type==1){
        res.data.map((item: any) => {
          option2.push({
            text: item.heatExchangeStationName,
            value: item.hrzID
          })
        })
        this.setData({
          option2: option2
        })
      }else{
        res.data.map((item: any) => {
          option2.push({
            text: item.assemblingUnitName,
            value: item.jzID
          })
        })
        this.setData({
          option2: option2
        })
      }

    })
    this.onSearch()
  },
  onSearch() {
    // 重置分页参数和数据
    this.setData({
      page: 1,
      list: [],
      hasMore: true
    });

    // 加载数据
    this.loadData();
    // const comID = this.data.comID;
    // const type = this.data.type;
    // const page = this.data.page;
    // const limit = this.data.limit;
    // const search = this.data.search;
    // natApi.getQuery(page, limit, comID, type, search).then((res: any) => {
    //   this.setData({
    //     list: res.data.data,
    //     isFresh: false
    //   })
    //   console.log(res)
    // })
  },
  fresh() {
    this.setData({
      page: 1,
      list: [],
      hasMore: true
    });
    this.loadData();
  },
  onDetail(e: any) {
    const type = this.data.type
    const { id } = e.currentTarget.dataset
    wx.navigateTo({
      url: '/subpackagesA/pages/detailLedger/detailLedger?type=' + type + '&id=' + id
    })
  },
  loadData() {
    if (this.data.loading || !this.data.hasMore) return;
    this.setData({ loading: true, isFresh: true });
    const { page, limit, comID, type, search,equipID } = this.data;
    natApi.getQuery(page, limit, comID, type, search,equipID).then((res: any) => {
      const newData = res.data.data || [];//追加新数据
      // 判断是否有更多数据
      const hasMore = newData.length === limit;
      this.setData({
        list: this.data.list.concat(newData),
        page: this.data.page + 1,
        hasMore: hasMore,
        isFresh: false
      })
    }).catch((error: any) => {
      console.log('加载数据失败', error)
    }).finally(() => {
      this.setData({ loading: false, isFresh: false }); // 结束加载状态
    })
  },
  bindscrolltolower(){
    this.loadData(); // 页面加载时加载初始数据
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    this.onSearch();
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})